package com.ruoyi.cms.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cms.domain.CmsDocument;
import com.ruoyi.cms.service.ICmsDocumentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 平台相关文档Controller
 * 
 * @author zouhuu
 * @date 2022-08-04
 */
@RestController
@RequestMapping("/cms/cmsDocument")
public class CmsDocumentController extends BaseController
{
    @Autowired
    private ICmsDocumentService cmsDocumentService;

    /**
     * 查询平台相关文档列表
     */
    @PreAuthorize("@ss.hasPermi('cms:cmsDocument:list')")
    @GetMapping("/list")
    public TableDataInfo list(CmsDocument cmsDocument)
    {
        startPage();
        List<CmsDocument> list = cmsDocumentService.selectCmsDocumentList(cmsDocument);
        return getDataTable(list);
    }

    /**
     * 导出平台相关文档列表
     */
    @PreAuthorize("@ss.hasPermi('cms:cmsDocument:export')")
    @Log(title = "平台相关文档", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmsDocument cmsDocument)
    {
        List<CmsDocument> list = cmsDocumentService.selectCmsDocumentList(cmsDocument);
        ExcelUtil<CmsDocument> util = new ExcelUtil<CmsDocument>(CmsDocument.class);
        util.exportExcel(response, list, "平台相关文档数据");
    }

    /**
     * 获取平台相关文档详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:cmsDocument:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(cmsDocumentService.selectCmsDocumentById(id));
    }

    /**
     * 新增平台相关文档
     */
    @PreAuthorize("@ss.hasPermi('cms:cmsDocument:add')")
    @Log(title = "平台相关文档", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmsDocument cmsDocument)
    {
        cmsDocument.setUserId(SecurityUtils.getUserId());
        cmsDocument.setDeptId(SecurityUtils.getDeptId());
        cmsDocument.setCreateBy(SecurityUtils.getUsername());
        return toAjax(cmsDocumentService.insertCmsDocument(cmsDocument));
    }

    /**
     * 修改平台相关文档
     */
    @PreAuthorize("@ss.hasPermi('cms:cmsDocument:edit')")
    @Log(title = "平台相关文档", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmsDocument cmsDocument)
    {
        cmsDocument.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(cmsDocumentService.updateCmsDocument(cmsDocument));
    }

    /**
     * 删除平台相关文档
     */
    @PreAuthorize("@ss.hasPermi('cms:cmsDocument:remove')")
    @Log(title = "平台相关文档", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cmsDocumentService.deleteCmsDocumentByIds(ids));
    }
}
